package ivo.util;
import ivo.service.impl.WfmService;

import java.io.InputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import org.apache.log4j.Logger;

public class Util {
	private static Logger logger = Logger.getLogger(Util.class);
	private static String strMySQLTimeFormat = "yyyy-MM-dd HH:mm:ss";
	
	public static String stackTraceToString(Exception excp) {
		StringWriter sw = new StringWriter();
		PrintWriter pw = new PrintWriter(sw, true);
		excp.printStackTrace(pw);
		pw.flush();
		sw.flush();
		return sw.toString();
	}
	
	public static boolean isSpaceCheck(String str) {
		if (null == str || str.length() <= 0 || ("").equals(str)) {
			return true;
		}
		return false;
	}
	
	public static String getChgTime(String time,Integer c) {
		long nm = c*1000;// 计算C的毫秒数
		long count;
		String chgTime = "";
		SimpleDateFormat sd = new SimpleDateFormat(strMySQLTimeFormat);
		try {
			count = sd.parse(time).getTime() + nm;
			chgTime = sd.format(count);
		} catch (ParseException ex) {
			logger.error(Util.stackTraceToString(ex));
		}
		return chgTime;
	}
	
	public static String readLoadCrrUseTimeProperties(String stk_id){
		String useTime = "";
    	Properties prop =  new  Properties(); 
    	try{
    		InputStream in =  WfmService.class.getResourceAsStream("/load_crr_use_time.properties");
    		prop.load(in);
    		useTime =String.valueOf(prop.getProperty (stk_id));
    	}catch (Exception e){
    		e.printStackTrace();
    	}
    	return useTime;
	}
	
	public static Boolean CompareTime(String time1 , String time2, String waitTime){
		long wt = Integer.parseInt(waitTime, 10) * 60 * 1000;
		long countMin;
		long countMax;
		long count;
		SimpleDateFormat sd = new SimpleDateFormat(strMySQLTimeFormat);
		try {
			countMin = sd.parse(time1).getTime();
			countMax = sd.parse(time1).getTime() + wt;
			count = sd.parse(time2).getTime();
			if(count >= countMin && count <= countMax){
				return true;
			}else{
				return false;
			}
		} catch (ParseException ex) {
			logger.error(Util.stackTraceToString(ex));
		}
		return false;
	}
	
	public static String Bay2Stk(String bay_id){
		Map<String, String> bayMap = new HashMap<String, String>();
		bayMap.put("1AS01", "A0STO010");
		bayMap.put("1AS02", "A0STO020");
		bayMap.put("1AS03", "A0STO030");
		bayMap.put("1AS04", "A0STO040");
		bayMap.put("1AS05", "A0STO050");
		bayMap.put("1AS06", "A0STO060");
		bayMap.put("1AS07", "A0STO070");
		bayMap.put("1AS08", "A0STO080");
		bayMap.put("1AS09", "A0STO090");
		bayMap.put("1AS10", "A0STO100");
		bayMap.put("1AS11", "A0STO110");
		bayMap.put("1AS12", "A0STO120");
		bayMap.put("1AS13", "A0STO130");
		bayMap.put("1AS14", "A0STO140");
		bayMap.put("1AS15", "A0STO150");
		bayMap.put("1AS16", "A0STO160");
		bayMap.put("1AS17", "A0STO170");
		
		if (bayMap.containsKey(bay_id)){
			return bayMap.get(bay_id);
		} else {
			return bay_id;
		}
	}
}
